﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S_UserInfor.Infrasturctre
{
    public class DapperRepostitory<T> : IDapperRepository<T> where T : class
    {
        public DapperContext _dapper;

        public DapperRepostitory(DapperContext dapper)
        {
            _dapper = dapper;
        }
        //添加
        public async Task<int> Insert(T entity, string insertSql)
        {
            IDbConnection dbConnection = _dapper.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }
                    return await dbConnection.ExecuteAsync(insertSql, entity);
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }

        //删除
        public async Task<int> Delete(int Id, string deleteSql)
        {
            IDbConnection dbConnection = _dapper.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    return await dbConnection.ExecuteAsync(deleteSql, new { Id = Id });
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }
        
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="selectOneSql"></param>
        /// <returns></returns>
        public async Task<T> GetOne(int Id, string selectOneSql)
        {

            IDbConnection dbConnection = _dapper.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    var result = await dbConnection.QueryFirstOrDefaultAsync<T>(selectOneSql, new { Id = Id });
                    return result;
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }

        //显示加分页
        public async Task<List<T>> SelectAsync(int PageSize, string selectSql)
        {
            IDbConnection dbConnection = _dapper.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    return await Task.Run(() => dbConnection.Query<T>(selectSql,new { PageSize = PageSize }).ToList());
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }

        //修改
        public async Task<int> Update(T entity, string updateSql)
        {
            IDbConnection dbConnection = _dapper.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    return await dbConnection.ExecuteAsync(updateSql, entity);
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }
    }
}
